/**
 * @(#)UploadFile.java, 2020/9/29.
 * <p/>
 * Copyright 2020 Netease, Inc. All rights reserved.
 * NETEASE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.lhh.myspringboot.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.github.pagehelper.PageInfo;
import com.lhh.myspringboot.bean.Admin;
import com.lhh.myspringboot.service.AdminService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * @author lvhouhou(lvhouhou @ 163.com)
 */

@RestController
public class UploadFile {


    @Autowired
    private AdminService adminService;

    @RequestMapping("/uploadFile2")
    public String uploadFile(@RequestParam MultipartFile file) throws IOException {


        // File dest = new File("G:\\temp\\" + file.getOriginalFilename());
        File dest = new File("D:"+File.separator + "devData"+ File.separator + "项目测试数据存放"+ File.separator + "image" + File.separator + file.getOriginalFilename());
        if (!file.isEmpty()){

            //   使用MultipartFile的transferTo方法将上传的文件 保存到本地 Transfer the received file to the given destination file.
            file.transferTo(dest);
            System.out.println("111");
            return "OK";
        }
        return "failed";
    }

    @RequestMapping("/uploadFile")
    public String uploadFile2(@RequestParam MultipartFile file) throws IOException {

        file.transferTo(new File("D:"+File.separator + "devData"+ File.separator + "项目测试数据存放"+ File.separator + "image" + File.separator + file.getOriginalFilename()));

        System.out.println("*****");
        return "OK";
    }



    //excel下载
    @RequestMapping("excel/download")
    public void  dataUoload(HttpServletRequest request, HttpServletResponse response) throws IOException {

        //  1.获取要导出的数据
        PageInfo<Admin> adminList = adminService.getAdminList(1, 5);
        List<Admin> list =adminList.getList();
        System.out.println(list);
        //2.用easypoi的Excel导出工具类导出Excel，四个导出参数，标题，表名，实体类对象，数据集合
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("admin表所有数据", "admin"), Admin.class, list);
        //3.通过文件对象写输出流到磁盘
        //workbook.write(new FileOutputStream(new File( "D:\\devData\\项目测试数据存放\\file\\admin.xls")));
         /*或通过浏览器下载*/
        // 告诉浏览器用什么软件可以打开此文件
        // response.setHeader("content-Type", "application/vnd.ms-excel");
        response.setHeader("content-Type", "application/x-xls");

        // 下载文件的名称
        String fileName = "admin.xls";

        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        workbook.write(response.getOutputStream());

         /*Content-disposition 是 MIME 协议的扩展，MIME 协议指示 MIME 用户代理如何显示附加的文件。
        Content-disposition其实可以控制用户请求所得的内容存为一个文件的时候提供一个默认的文件名，文件直接在浏览器上显示或者在访问时弹出文件下载对话框。*/

          /*  Content-type
        内容类型，一般是指网页中存在的Content-Type，用于定义网络文件的类型和网页的编码，决定浏览器将以什么形式、什么编码读取这个文件。
        文件扩展名与 Content-type 的对应关系，参见 http://tool.oschina.net/commons  */
    }

    //Excel文件数据上传
    @RequestMapping("excel/upload")
    public void dateUpload2(@RequestParam MultipartFile file) throws Exception {
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);

        //  MultipartFile类型的file，直接通过file.getInputStream()得到一个InputStream
        List<Admin> list = ExcelImportUtil.importExcel(file.getInputStream(), Admin.class, params);
        for (Admin admin : list) {
            System.out.println(admin);
        }
    }
}
